package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotIndustrySensorRelService;
import com.lechi.web.system.domain.IotIndustrySensorRel;
import com.lechi.web.system.mapper.iot.IotIndustrySensorRelMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 行业关联观测要素关系Service业务层处理
 *
 * @author yangguang
 * @date 2024 -05-17
 */
@Service
@RequiredArgsConstructor
public class IotIndustrySensorRelServiceImpl implements IIotIndustrySensorRelService {

    private final IotIndustrySensorRelMapper iotIndustrySensorRelMapper;

    /**
     * 查询行业关联观测要素关系
     *
     * @param id 行业关联观测要素关系主键
     * @return 行业关联观测要素关系
     */
    @Override
    public IotIndustrySensorRel selectIotIndustrySensorRelById(Long id) {
        return iotIndustrySensorRelMapper.selectIotIndustrySensorRelById(id);
    }

    /**
     * 查询行业关联观测要素关系列表
     *
     * @param iotIndustrySensorRel 行业关联观测要素关系
     * @return 行业关联观测要素关系
     */
    @Override
    public List<IotIndustrySensorRel> selectIotIndustrySensorRelList(IotIndustrySensorRel iotIndustrySensorRel) {
        try {
            return iotIndustrySensorRelMapper.selectIotIndustrySensorRelList(iotIndustrySensorRel);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询行业关联观测要素关系列表", method, iotIndustrySensorRel));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增行业关联观测要素关系
     *
     * @param iotIndustrySensorRel 行业关联观测要素关系
     */
    @Override
    public void insertIotIndustrySensorRel(IotIndustrySensorRel iotIndustrySensorRel) {
        try {
            iotIndustrySensorRelMapper.insertIotIndustrySensorRel(iotIndustrySensorRel);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增行业关联观测要素关系", method, iotIndustrySensorRel));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改行业关联观测要素关系
     *
     * @param iotIndustrySensorRel 行业关联观测要素关系
     */
    @Override
    public void updateIotIndustrySensorRel(IotIndustrySensorRel iotIndustrySensorRel) {
        try {
            iotIndustrySensorRelMapper.updateIotIndustrySensorRel(iotIndustrySensorRel);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改行业关联观测要素关系", method, iotIndustrySensorRel));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 批量删除行业关联观测要素关系
     *
     * @param ids 需要删除的行业关联观测要素关系主键
     */
    @Override
    public void deleteIotIndustrySensorRelByIds(Long[] ids) {
        try {
            iotIndustrySensorRelMapper.deleteIotIndustrySensorRelByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除行业关联观测要素关系", method, ids));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 批量删除行业关联观测要素关系
     *
     * @param iotIndustrySensorRel 需要删除的行业关联观测要素关系主键
     */
    @Override
    public void deleteIotIndustrySensorRelByIndustryId(IotIndustrySensorRel iotIndustrySensorRel) {
        try {
            iotIndustrySensorRelMapper.deleteIotIndustrySensorRelByIndustryId(iotIndustrySensorRel);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过行业ID删除行业关联观测要素关系", method, iotIndustrySensorRel));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }
}
